package krow.dev.gunpobus;

import krow.dev.gunpobus.Constants.Invoke;
import krow.dev.gunpobus.Constants.Value;
import krow.dev.gunpobus.control.database.DataController;
import krow.dev.gunpobus.control.http.HtmlResponseListener;
import krow.dev.gunpobus.control.http.RequestManager;
import krow.dev.gunpobus.control.parser.NodeParser;
import krow.dev.gunpobus.model.NodeItem;
import krow.dev.gunpobus.view.RetryViewWrapper;
import krow.dev.gunpobus.view.adapter.NodeListAdapter;
import krow.dev.gunpobus.view.dialog.LoadDialog;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.RelativeLayout.LayoutParams;

import com.android.volley.Request;
import com.android.volley.VolleyError;

public class NodeActivity extends Activity implements OnClickListener {

	private NodeListAdapter mNodeListAdapter;
	private ListView mNodeListView;
	private TextView mTitleView;
	private View mFavoriteView;
	private View mRefreshView;
	private ViewGroup mListLayout;
	private RetryViewWrapper mRetryViewWrapper;
	
	private DataController mDataController;
	private Dialog mDialog;

	private String mRouteId;
	private String mRouteName;
	private int mRouteType;

	private Request<?> mRequest;
	private Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_node);

		mDataController = GlobalStore.getInstance().getDataController();
		
		Intent intent = getIntent();
		if (intent != null) {
			mRouteId = intent.getStringExtra(Invoke.KEY_ROUTE_ID);
			mRouteName = intent.getStringExtra(Invoke.KEY_ROUTE_NAME);
			mRouteType = intent.getIntExtra(Invoke.KEY_ROUTE_TYPE, Value.TYPE_ROUTE_VILLAGE);
		}
		
		initializeView();
	}

	private void initializeView() {

		mNodeListAdapter = new NodeListAdapter(getApplicationContext());
		mNodeListAdapter.setRouteType(mRouteType);
		mNodeListView = (ListView) findViewById(R.id.listview_node);
		mNodeListView.setOnItemClickListener(mItemClickListener);
		mNodeListView.setAdapter(mNodeListAdapter);

		mTitleView = (TextView) findViewById(R.id.text_title);
		mTitleView.setText(mRouteName);
		
		mFavoriteView = findViewById(R.id.layout_favorite);
		mFavoriteView.setOnClickListener(this);
		
		mRefreshView = findViewById(R.id.layout_refresh);
		mRefreshView.setOnClickListener(this);
		
		if(mDataController.isFavorited(Value.TYPE_FAVORITE_ROUTE, mRouteId)){
			mFavoriteView.setSelected(true);
		}else{
			mFavoriteView.setSelected(false);
		}
		
		mListLayout = (ViewGroup) findViewById(R.id.layout_list);
		
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
				LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
		
		mRetryViewWrapper = new RetryViewWrapper(getApplicationContext());
		mRetryViewWrapper.setRetryClickListener(mRetryClickListener);
		mListLayout.addView(mRetryViewWrapper.getView(), layoutParams);
		
		findViewById(R.id.layout_title).setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		requestNode();
	}

	@Override
	protected void onPause() {

		super.onPause();
	}

	@Override
	protected void onDestroy() {
		if(mRequest != null){
			mRequest.cancel();
		}
		super.onDestroy();
	}
	
	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.scale_in, R.anim.right_out);
	}
	
	@Override
	public void onClick(View view) {

		switch (view.getId()) {
		case R.id.layout_favorite:
			if(mFavoriteView.isSelected()){
				mFavoriteView.setSelected(false);
				mDataController.deleteFavoriteItem(
						Value.TYPE_FAVORITE_ROUTE,
						mRouteId);
			}else{
				mFavoriteView.setSelected(true);
				mDataController.insertFavoriteItem(
						Value.TYPE_FAVORITE_ROUTE,
						mRouteName,
						mRouteId,
						null,
						mRouteType);
			}
			break;
		case R.id.layout_refresh:
			requestNode();
			break;
		case R.id.layout_title:
			finish();
			break;
		}
	}

	private void requestNode() {
		if(mRetryViewWrapper != null){
			mRetryViewWrapper.setVisibility(View.GONE);
		}
		if (mRouteId == null) {
			return;
		}

		if (mDialog != null) {
			mDialog.dismiss();
		}
		mDialog = new LoadDialog(this);
		mDialog.show();

		mRequest = RequestManager.requestNode(mRouteId, mResponseListener);
	}

	private HtmlResponseListener<NodeParser> mResponseListener = new HtmlResponseListener<NodeParser>() {

		@Override
		public void onResponse(NodeParser response) {
			if (mDialog != null) {
				mDialog.dismiss();
			}

			mNodeListAdapter.setNodeItemList(response.getNodeItemList());
			mNodeListAdapter.notifyDataSetChanged();
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			mHandler.postDelayed(mRequestFailureRunnable, 1000);
		}
	};
	
	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if(view.getTag() == null){
				return ;
			}
			
			NodeItem nodeItem = (NodeItem) view.getTag();
			
			Intent intent = new Intent(NodeActivity.this, ScheduleActivity.class);
			intent.putExtra(Invoke.KEY_STATION_NAME, nodeItem.getName());
			intent.putExtra(Invoke.KEY_STATION_ID, nodeItem.getId());
			intent.putExtra(Invoke.KEY_STATION_SERIAL, nodeItem.getSerialNumber());
			startActivity(intent);
			overridePendingTransition(R.anim.right_in, R.anim.scale_out);
		}
	};
	
	private Runnable mRequestFailureRunnable = new Runnable() {
		
		@Override
		public void run() {
			if (mDialog != null) {
				mDialog.dismiss();
			}
			mNodeListAdapter.clear();
			mNodeListAdapter.notifyDataSetChanged();
			mRetryViewWrapper.setVisibility(View.VISIBLE);
		}
	};
	
	private View.OnClickListener mRetryClickListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View view) {
			requestNode();
		}
	};
}
